package com.example.studentsRoom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.studentsRoom.entity.*;
import com.example.studentsRoom.form.SearchForm;
import com.example.studentsRoom.mapper.*;
import com.example.studentsRoom.service.IAbsentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.studentsRoom.vo.AbsentVo;
import com.example.studentsRoom.vo.BuildAndDormitoryVo;
import com.example.studentsRoom.vo.PageVo;
import com.example.studentsRoom.vo.TransitionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-07-19
 */
@Service
public class AbsentService extends ServiceImpl<AbsentMapper, Absent> implements IAbsentService {

    @Autowired
    private AbsentMapper absentMapper;

    @Autowired
    private BuildingMapper buildingMapper;

    @Autowired
    private DormitoryMapper dormitoryMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private DormitoryAdminMapper dormitoryAdminMapper;

    @Override
    public PageVo listPage(Integer page, Integer size) {
        Page<Absent> absentPage=new Page<>(page,size);
        Page<Absent> resultPage = absentMapper.selectPage(absentPage, null);

        PageVo pageVo= BuildAndDormitoryVo.getAbsent(resultPage,buildingMapper,dormitoryAdminMapper,
                dormitoryMapper,studentMapper,absentMapper);
        return pageVo;
    }

    @Override
    public PageVo search(SearchForm searchForm) {
        Integer id=0;
        if (searchForm.getKey().equals("dor_id")&&searchForm.getValue()!=null){
            QueryWrapper<Dormitory> dormitoryQueryWrapper=new QueryWrapper<>();
            dormitoryQueryWrapper.like("dor_name",searchForm.getValue());

            Dormitory dormitory = dormitoryMapper.selectOne(dormitoryQueryWrapper);

            id=dormitory.getDorId();
        }else if (searchForm.getKey().equals("b_id")&&searchForm.getValue()!=null){
            QueryWrapper<Building> buildingQueryWrapper=new QueryWrapper<>();
            buildingQueryWrapper.like("b_msg",searchForm.getValue());

            Building building = buildingMapper.selectOne(buildingQueryWrapper);

            id=building.getBId();
        }

        //模糊查询加分页
        Page<Absent> absentPage=new Page<>(searchForm.getPage(),searchForm.getSize());
        Page<Absent> resultPage = null;

        //判断value值是不是null
        if (searchForm.getValue().equals("")){
            resultPage = this.absentMapper.selectPage(absentPage, null);
        }else{
            QueryWrapper<Absent> queryWrapper=new QueryWrapper<>();
            queryWrapper.like(searchForm.getKey(),id);
            resultPage = this.absentMapper.selectPage(absentPage, queryWrapper);
        }

        PageVo pageVo= BuildAndDormitoryVo.getAbsent(resultPage,buildingMapper,dormitoryAdminMapper,
                dormitoryMapper,studentMapper,absentMapper);

        return pageVo;
    }
}
